package Gather.MapExercise.MapDome;

import java.util.*;
import java.util.function.Consumer;

/**
 * ClassName:MapText003
 * Package:Gather.MapExercise
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/2/20-16:22
 * @Version:v1.0
 */
public class MapText003 {
    /*Map集合的遍历方式
     * 1.键找值
     * 2.键值对
     * 3.Lambda表达式*/
    public static void main(String[] args) {
        //创建Map集合的对象
        Map<String, String> map = new HashMap<>();
        //添加元素
        map.put("郭靖", "黄蓉");
        map.put("杨过", "小龙女");
        map.put("韦小宝", "沐剑屏");
        //通过键找值
        /*通过所有的键,把这些键放到一个单列集合当中*/
        Set<String> keys = map.keySet();
        //遍历单列集合,得到每一个键(1.迭代器 2.增强for 3.Lambda表达式)

        //增强for遍历
        System.out.println("==增强for遍历==");
        for (String key : keys) {
//            System.out.println(key);
            //利用map集合中的键获取对应的值  get
            String value = map.get(key);
            System.out.println(key + "=" + value);
        }
        //迭代器遍历
        System.out.println("==迭代器遍历==");
        Iterator<String> it = keys.iterator();
        while (it.hasNext()) {
            String key = it.next();
            String value = map.get(key);
            System.out.println(key + "=" + value);
        }
        //Lambda表达式遍历
        System.out.println("==Lambda表达式遍历==");
        keys.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                String value = map.get(s);
                System.out.println(s + "=" + value);
            }
        });
    }
}